package soapClientDemo;//start extract SoapInvocationHandler
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;

import java.lang.reflect.*;
import java.net.URL;
import java.util.Vector;

public class SoapInvocationHandler implements InvocationHandler {

    public static Object createSoapProxy( Class[] interfaces,         //#1
					  URL serverURL,              //#1
					  String serviceName,         //#1
					  String encoding )           //#1
    {                                                                 //#1
	SoapInvocationHandler handler = new SoapInvocationHandler();  //#1
	handler.serverURL = serverURL;                                //#1
	handler.serviceName = serviceName;                            //#1
	handler.encoding = encoding;                                  //#1
	                                                              //#1
	return Proxy.newProxyInstance(                                //#1
		     SoapInvocationHandler.class.getClassLoader(),    //#1
		     interfaces,                                      //#1
		     handler );                                       //#1
    }                                                                 //#1

    private URL serverURL;                                            //#2
    private String serviceName;                                       //#2
    private String encoding;                                          //#2

    private SoapInvocationHandler() {}

    public Object invoke( Object proxy, Method method, Object[] args )//#3
	throws Throwable 
    {
	Call call = new Call();                                       //#4
	call.setTargetObjectURI( serviceName );                       //#4
	call.setMethodName( method.getName() );                       //#4

	Vector params = new Vector();                                 //#5
	Class[] types = method.getParameterTypes();                   //#5
	for ( int i = 0; i < args.length; i++ ) {                     //#5
	    params.add(                                               //#5
		  new Parameter( "p"+i, types[i], args[i], encoding ) //#5
		  );                                                  //#5
	}                                                             //#5
	call.setParams( params );                                     //#5

	Response resp = call.invoke( serverURL, "" );                 //#6

	if ( resp.generatedFault() ) {                                //#7
	    throw new RuntimeException( resp.getFault().toString() ); //#7
	}                                                             //#7
	else {                                                        //#7
	    return resp.getReturnValue().getValue();                  //#7
	}                                                             //#7
    }
}
//stop extract SoapInvocationHandler
